package org.ejde.sciencelogs;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JPanel;

/**
 * This class contains the attributes and behaviors for 
 * JComponents related to top-level tabbed panel.
 *  
 * @author Eric Martin
 * @version 0.1
 */
public class tabPanel extends JPanel {
	public subPanel[] subpanels;

	/**
	 * Constructor method
	 * 
	 * @param ini configuration information.
	 * @param factory formfield database.
	 * @param udp UDP-based logging client.
	 * @param smack XMPP-based logging client.
	 * @param panelID the ID of the current panel.
	 */
	public tabPanel(iniParser ini, fieldFactory factory, UdpXmitter udp,
			smackClient smack, int panelID) {
		
		/**
		 * The number of subpanels in this tab
		 */
		int Nsubpanels = ini.subpanelCount(panelID);
		
		/**
		 * The center panel 
		 */
		JPanel mpanel;
		
		// Initialized the layout manager
		this.setLayout(new BorderLayout());

		// First create a panel for the overhead fields and add them.
		formField[] pf = factory.getPanelFields(panelID);
		if (pf.length != 0) {
			// this.setLayout(new GridLayout(Nsubpanels+1,1,10,10));
			JPanel tpanel = new JPanel(new FlowLayout());
			for (int n = 0; n < pf.length; n++) {
				tpanel.add(pf[n].getLabel());
				// Get the appropriate action
				switch (pf[n].type) {
				case formField.TEXT_FIELD:
					tpanel.add(pf[n].getTextField());
					break;
				case formField.LIST_FIELD:
					tpanel.add(pf[n].getListField());
					break;
				case formField.TEXTAREA_FIELD:
					tpanel.add(pf[n].getTextAreaField());
					break;
				}
			}

			// Add the overhead fields
			this.add("North", tpanel);
		}

		// Initialize the center panel. 
		mpanel = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;

		// Initialize the subpanels
		subpanels = new subPanel[Nsubpanels];
		for (int n = 0; n < Nsubpanels; n++) {
			// for grid bag layout
			c.weightx = 0.5;
			c.gridx = 0;
			c.gridy = n;
			subpanels[n] = new subPanel(ini, factory, udp, smack, panelID, n);
			mpanel.add(subpanels[n], c);
		}
		this.add("Center", mpanel);
	}
}
